using EasyModbus;
using System.IO.Ports;

namespace nmodbus4测试
{
    public partial class Form1 : Form
    {


        /// <summary>
        /// 私有ModbusRTU主站字段
        /// </summary>
        private static ModbusClient master;


        public Form1()
        {
            InitializeComponent();

            //创建ModbusRTU主站实例
            master = new ModbusClient("COM2");

            //设定串口参数
            master.Baudrate = 115200;
            master.UnitIdentifier = 1;
            master.Parity = Parity.None;
            master.StopBits = StopBits.One;
            master.ConnectionTimeout = 5000;
            master.Connect();

        }





        /// <summary>
        /// 写入单个线圈
        /// </summary>
        private void WriteSingleCoil(object sender, EventArgs e)
        {
            //master.WriteSingleCoil(1, false);
            //List<bool> result = new List<bool>() { true, false, true, false };
            //master.WriteMultipleCoils(1, result.ToArray());

            master.WriteMultipleRegisters(1, new int[] { 18, 28, 38, 48, 58, 68 });
            //master.Disconnect();
        }

        /// <summary>
        /// 批量写入线圈
        /// </summary>
        private void WriteArrayCoil(object sender, EventArgs e)
        {
            List<bool> result = new List<bool>() { true, false, true, false };
            master.WriteMultipleCoils(0, result.ToArray());
        }




    }
}
